<?php
namespace app\company\logic;

use app\company\logic\JiPush;
use think\Controller;

class Message extends Controller{
    /**
     * $title 消息标题
     * $user_id  用户id
     * $cate_type  消息类型  1审核消息 2任务消息 3公告消息 4积分奖扣消息 5催审 7请假消息
     * $type  数据类型 1公告  2任务 3积分奖扣 4请假
     * $relation_id  数据类型关联表id  包含 任务id,公告id,积分奖扣id,请假申请id
     */
    static function send_message($title,$user_id,$cate_type,$type,$relation_id){
        if($type == 1){
            $res = M('com_notice a')->where('a.id',$relation_id)
                ->join('users b','b.id=a.record_user_id','left')
                ->join('users c','c.id=a.first_check_user_id','left')
                ->join('users d','d.id=a.last_check_user_id','left')
                ->field('a.*,b.name record_name,c.name first_name,d.name last_name')
                ->find();
            $com_id = $res['com_id'];
            $name = $res['name'];
            $record_name = $res['record_name'];
            $first_name = $res['first_name'];
            $last_name = $res['last_name'];
        }elseif ($type == 2){
            $res = M('com_task a')->where('a.id',$relation_id)
                ->join('users b','b.id=a.record_user_id','left')
                ->join('users c','c.id=a.first_check_user_id','left')
                ->join('users d','d.id=a.last_check_user_id','left')
                ->field('a.*,b.name record_name,c.name first_name,d.name last_name')
                ->find();
            $com_id = $res['com_id'];
            $name = $res['name'];
            $record_name = $res['record_name'];
            $first_name = $res['first_name'];
            $last_name = $res['last_name'];
        }elseif ($type == 3){
            $res = M('com_integral_prize_buckle a')->where('a.id',$relation_id)
                ->join('users b','b.id=a.record_user_id','left')
                ->join('users c','c.id=a.first_check_user_id','left')
                ->join('users d','d.id=a.last_check_user_id','left')
                ->field('a.*,b.name record_name,c.name first_name,d.name last_name')
                ->find();
            $com_id = $res['com_id'];
            $name = $res['theme'];
            $record_name = $res['record_name'];
            $first_name = $res['first_name'];
            $last_name = $res['last_name'];
        }elseif ($type == 4){
            $res = M('com_leave a')->where('a.id',$relation_id)
                ->join('users b','b.id=a.user_id','left')
                ->join('users c','c.id=a.first_check_user_id','left')
                ->join('users d','d.id=a.last_check_user_id','left')
                ->field('a.*,b.name record_name,c.name first_name,d.name last_name')
                ->find();
            $com_id = $res['com_id'];
            $name = '请假';
            $record_name = $res['record_name'];
            $first_name = $res['first_name'];
            $last_name = $res['last_name'];
        }
        $data = [
            'com_id'=>$com_id,
            'user_id'=>$user_id,
            'cate_type'=>$cate_type,
            'type'=>$type,
            'title'=>$title,
            'relation_id'=>$relation_id,
            'name'=>$name,
            'record_name'=>$record_name,
            'first_name'=>$first_name,
            'last_name'=>$last_name,
            'create_time'=>time(),
        ];
        M('com_user_message')->insert($data);
        //极光推送
        switch ($cate_type){
            case 1:
                $title_push = '审核消息';
                break;
            case 2:
                $title_push = '任务消息';
                break;
            case 3:
                $title_push = '公告消息';
                break;
            case 4:
                $title_push = '积分奖扣消息';
                break;
            case 5:
                $title_push = '催审消息';
                break;
            case 7:
                $title_push = '请假消息';
                break;
        }
        $content = $title.'名称:'.$name.'记录人:'.$record_name.'初审人:'.$first_name.'终审人:'.$last_name;
        try {
            JiPush::push($user_id, $title_push, $content);
        }catch (\Exception $e){
//            echo $e->getMessage();
        }
    }
    //批量发送消息
    static public function send_message_list($data){
        if(empty($data)){
            return false;
        }
        foreach ($data as $key=>$value){
            self::send_message($value['title'],$value['user_id'],$value['cate_type'],$value['type'],$value['relation_id']);
        }
    }
    //发送奖扣任务消息
    static public function send_prize_buckle_task_msg($data){
        M('com_user_message')->insert($data);
    }

}
